import { _decorator, BoxCollider2D, Component, Contact2DType, Director, director, Label, Node, RigidBody2D, v2, v3 } from 'cc';
import { gameMgr } from './GameMgr';
import { getDistance, trackTarget, trackTargetMove } from './utils';
import { Player } from './Player';
import { biontTypeEnum } from './Enum';
const { ccclass, property } = _decorator;

@ccclass('Biont')
export class Biont extends Component {
    
    biontType=0;
    biontId=0;

    moveSpeed=270

    targetPos=null;

    tempPos=v3();

    isIndependent=true

    canPZ = true


    init(_biontId, _biontType, _isIndependent = true,isMutualCollisionEvent=false) {
        this.canPZ = true
        this.biontId = _biontId
        this.isIndependent = _isIndependent
        this.biontType = _biontType;

        if (_isIndependent) {
            this.targetPos = this.getNextTargetPos();
        }


        if (isMutualCollisionEvent) {
        this.node.getComponent(BoxCollider2D).on(Contact2DType.BEGIN_CONTACT,this.beginContact)
            
        }

    }

    //注册玩家碰撞
    registerEventPlayerCod(){
        this.node.getComponent(BoxCollider2D).on(Contact2DType.BEGIN_CONTACT,Player.ins.beginContact)

    }

    beginContact(selfC, otherC, contact) {
            director.once(Director.EVENT_AFTER_PHYSICS, () => {
                    if (otherC.tag!=1001 && selfC.tag!=1001) {
    
                        if (getDistance(otherC.node.getWorldPosition().x, otherC.node.getWorldPosition().y, Player.ins.node.getWorldPosition().x, Player.ins.node.getWorldPosition().y) < 1500) {
                            
                            if (otherC.node.getComponent(Biont).biontType == selfC.node.getComponent(Biont).biontType) {

                                return;
                            }
                            if (otherC.node.getComponent(Biont).biontType > selfC.node.getComponent(Biont).biontType) {

                                gameMgr.ins.currExistBiontsTotalNum -= 1
                                gameMgr.ins.existBiontsDic.delete(selfC.node.getComponent(Biont).biontId)
                                selfC.node.destroy();
                            }
                            else {

                                gameMgr.ins.currExistBiontsTotalNum -= 1
                                gameMgr.ins.existBiontsDic.delete(otherC.node.getComponent(Biont).biontId)
                                otherC.node.destroy();
                            }
                        }
    
                }
            })

            if (otherC.tag==1001) {
                console.log('碰撞了')
                Player.ins.beginContact(otherC,selfC,contact);
            }

        // if (getDistance(otherC.node.getWorldPosition().x, otherC.node.getWorldPosition().y, Player.ins.node.getWorldPosition().x, Player.ins.node.getWorldPosition().y) < 500) {
        //     console.log("22")
        // }
       


    }

    protected update(dt: number): void {
        if (gameMgr.ins.isGameOver==false) {
            if (this.isIndependent) {
                if (this.targetPos) {
                    this.biontTrackTargetMove(this.targetPos,this.node,this.moveSpeed,dt)
                }
            }
            if (this.isIndependent) {
                let dis = 50

                if (Player.ins.currControlBiontType==biontTypeEnum.wugui||this.biontType==biontTypeEnum.qingwa) {
                    dis = 90
                }
                if (Player.ins.currControlBiontType==biontTypeEnum.wugui&&this.biontType==biontTypeEnum.wugui) {
                    dis = 110
                }
                if (Player.ins.currControlBiontType==biontTypeEnum.jinyu0||this.biontType==biontTypeEnum.jinyu0) {
                    dis = 130
                    if (Player.ins.currControlBiontType==biontTypeEnum.jinyu0&&this.biontType==biontTypeEnum.jinyu0) {
                    dis = 150
                    }
                }
                if (Player.ins.currControlBiontType==biontTypeEnum.jinyu1||this.biontType==biontTypeEnum.jinyu1) {
                    dis = 130
                    if (Player.ins.currControlBiontType==biontTypeEnum.jinyu1&&this.biontType==biontTypeEnum.jinyu1) {
                    dis = 150
                    }
                }
                if (Player.ins.currControlBiontType == biontTypeEnum.liyu0 || this.biontType == biontTypeEnum.liyu0) {
                    dis = 140
                    if (Player.ins.currControlBiontType == biontTypeEnum.liyu0 && this.biontType == biontTypeEnum.liyu0) {
                        dis = 150
                    }
                }
                if (Player.ins.currControlBiontType==biontTypeEnum.liyu1||this.biontType==biontTypeEnum.liyu1) {
                    dis = 140
                    if (Player.ins.currControlBiontType==biontTypeEnum.liyu1&&this.biontType==biontTypeEnum.liyu1) {
                    dis = 150
                    }
                }
                if (Player.ins.currControlBiontType==biontTypeEnum.liyu2||this.biontType==biontTypeEnum.liyu2) {
                    dis = 140
                    if (Player.ins.currControlBiontType==biontTypeEnum.liyu2&&this.biontType==biontTypeEnum.liyu2) {
                    dis = 150
                    }
                }
                if (Player.ins.currControlBiontType == biontTypeEnum.shayu2 || this.biontType == biontTypeEnum.shayu2) {
                    dis = 180
                    if (Player.ins.currControlBiontType == biontTypeEnum.shayu2 && this.biontType == biontTypeEnum.shayu2) {
                        dis = 210
                    }
                }
                if (Player.ins.currControlBiontType == biontTypeEnum.shayu1 || this.biontType == biontTypeEnum.shayu1) {
                    dis = 180
                    if (Player.ins.currControlBiontType == biontTypeEnum.shayu1 && this.biontType == biontTypeEnum.shayu1) {
                        dis = 210
                    }
                }
                if (Player.ins.currControlBiontType == biontTypeEnum.changlong || this.biontType == biontTypeEnum.changlong) {
                    dis = 210
                    if (Player.ins.currControlBiontType == biontTypeEnum.changlong && this.biontType == biontTypeEnum.changlong) {
                        dis = 230
                    }
                }

                if (getDistance(this.node.getWorldPosition().x, this.node.getWorldPosition().y, Player.ins.node.getWorldPosition().x, Player.ins.node.getWorldPosition().y) < dis) {
                let cod=this.node.getComponent(BoxCollider2D);
                    console.log("dis",dis)
                    director.once(Director.EVENT_AFTER_PHYSICS, () => {
                        // if (cod.tag == 77) {
                        //     console.log("碰到道具了")
                        //     switch (otherC.getComponent(Prop).propType) {
                        //         case 0:
                        //             Player.ins.IsSpeedUp = true;
                        //             break;
                        //         case 1:
                        //             Player.ins.IsImmortal = true;
                        //             break;
                        //     }
                        //     otherC.node.destroy();
                        //     return
                        // }
                        if (cod.tag > Player.ins.currControlBiontType) {
                            if (Player.ins.IsImmortal == true) {
                                console.log("无敌不死")
                            }
                            else {
                                Player.ins.die();
                                console.log("游戏结束")
                            }
                        }
                        else {
                            Player.ins.captureBiont(cod.tag);
                            gameMgr.ins.existBiontsDic.delete(this.biontId)
                            this.node.destroy()
                        }
    
                    })
    
                }
            }  
        }
     
    }


    biontTrackTargetMove(targetPos, selfNode, speed, dt) {

        if (!targetPos) {
            this.destroy();
            return
        }

        let speedArr = trackTarget(targetPos.x, targetPos.y, selfNode.position.x, selfNode.position.y, speed * dt);

        selfNode.getPosition(this.tempPos);
        this.tempPos.x += speedArr[0]
        this.tempPos.y += speedArr[1]
        selfNode.setPosition(this.tempPos);

        selfNode.angle = speedArr[2] * (180 / Math.PI);


        //如果自己已经到达附近
        if (getDistance(selfNode.position.x, selfNode.position.y, targetPos.x, targetPos.y) < 50) {
            let nextTartgetPos = this.getNextTargetPos();
            this.targetPos = nextTartgetPos;
        }
    }

    getNextTargetPos() {
        let res = null;
        while (!res) {
            if (this.biontType <= 6) {
                let tempRes = v2(this.node.position.x + gameMgr.ins.randowNum(-2000, 2000), this.node.position.y + gameMgr.ins.randowNum(-2000, 2000))
                if (Math.abs(tempRes.x)>gameMgr.ins.mapBoundary.x||Math.abs(tempRes.x)>gameMgr.ins.mapBoundary.y) {
                    continue;
                }
                else{
                    res=tempRes;
                }
            }
            else {
                let tempRes = v2(this.node.position.x + gameMgr.ins.randowNum(-2500, 2500), this.node.position.y + gameMgr.ins.randowNum(-2500, 2500))
                if (Math.abs(tempRes.x)>gameMgr.ins.mapBoundary.x||Math.abs(tempRes.x)>gameMgr.ins.mapBoundary.y) {
                    continue;
                }
                else{
                    res=tempRes;
                }
            }

            return res;
        }

    }

}


